import os
import sys

# 为防止报错，手动指定一下PyQt5的系统变量
os.environ["QT_QPA_PLATFORM_PLUGIN_PATH"] = os.path.abspath("Python36/Lib/site-packages/PyQt5/Qt5/plugins")

import json
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton, QComboBox, QTextEdit, QLabel, QMessageBox, QCheckBox, QLineEdit
from utils import work

class MyWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle("最后纪元离线存档同步配装器BD工具")
        # 创建用于说明的文本
        self.label = QLabel("使用说明：\n1. 保证《最后纪元》停留在主界面，不要进入游戏，也不要停留在人物选择界面。\n2. 使用配套的油猴插件打开采蘑菇的配装器，点击“复制BD数据到剪贴板”\n3. 选择存档，并粘贴BD数据到下面的输入框中\n4. 点击“修改存档”按钮\n5. 此时方可进入人物选择界面开始游戏\n\n注意：修改存档有风险，背包物品会被覆盖，使用前请存好道具、备份存档。\n跨职业修改存档时，下方技能槽可能需要手动回复。")

        # 指定路径
        self.savesPathText = QLineEdit(self);
        self.savesPathText.setPlaceholderText("存档路径");
        self.savesPathText.setText("C:/Users/"+ os.getlogin() +"/AppData/LocalLow/Eleventh Hour Games/Last Epoch/Saves/");
        
        # 创建按钮
        self.openSaveButton = QPushButton("打开存档位置（手动备份用）")
        self.openSaveButton.clicked.connect(self.openSaveFile)

        # 创建下拉框
        self.comboBox = QComboBox()
        self.comboBox.placeholderText = "请选择存档"

        # 创建按钮
        self.button = QPushButton("读取存档")
        self.button.clicked.connect(self.refreshSaveFiles)

        # 创建多行输入框
        self.textEdit = QTextEdit()
        self.textEdit.setPlaceholderText("粘贴BD到这里")

        # 创建勾选框
        self.syncBdCheckBox = QCheckBox("同步BD")
        self.skipPlotCheckBox = QCheckBox("跳过剧情、解锁传送点、解锁地下城和竞技场难度、发现所有祝福")
        self.syncBdCheckBox.setChecked(True)        

        # 创建按钮
        self.textButton = QPushButton("修改存档")
        self.textButton.clicked.connect(self.modifySaveFile)

        # 创建超链接
        self.githubLink = QLabel("<a href=https://github.com/noobdawn/LastEpochBuildSynchronizeTools"">Github</a>")

        # 设置布局
        layout = QVBoxLayout()
        layout.addWidget(self.label)
        layout.addWidget(self.savesPathText)
        layout.addWidget(self.button)
        layout.addWidget(self.comboBox)
        layout.addWidget(self.openSaveButton)
        layout.addWidget(self.textEdit)
        layout.addWidget(self.syncBdCheckBox)
        layout.addWidget(self.skipPlotCheckBox)
        layout.addWidget(self.textButton)
        layout.addWidget(self.githubLink)
        self.setLayout(layout)
   

    def refreshSaveFiles(self):
        # 清空下拉框
        self.comboBox.clear()
        # 遍历C:/Users/{用户名}/AppData/LocalLow/Eleventh Hour Games/Last Epoch/Saves/
        path = self.savesPathText.text();
        files = os.listdir(path)
        for file in files:
            if file.find("CHARACTERSLOT") != -1 and not file.endswith(".bak") and not file.endswith("_temp"):
                with open(path + file, "r", encoding='utf-8') as f:
                    savedata = json.loads(f.read()[5:])
                    charname = savedata["characterName"]
                    slot = savedata["slot"]
                    self.comboBox.addItem(f"{charname} - {slot}")

    def openSaveFile(self):
        path = self.savesPathText.text();
        os.startfile(path)
        

    def modifySaveFile(self):
        # 获取下拉框选中的存档
        selected = self.comboBox.currentText()
        if selected == "":
            QMessageBox.information(self, "提示", "请选择存档")
            return
        slot = selected.split(" - ")[1]
        savefile = self.savesPathText.text() + f"1CHARACTERSLOT_BETA_{slot}"
        # 获取bd
        bdStr = self.textEdit.toPlainText()
        if len(bdStr) < 20:
            QMessageBox.information(self, "提示", "请填写正确的bd")
            return
        isSyncBdOnly = self.syncBdCheckBox.isChecked()
        isSkipPlot = self.skipPlotCheckBox.isChecked()
        work(bdStr, savefile, isSyncBdOnly, isSkipPlot)
        QMessageBox.information(self, "提示", "修改成功")
        self.textEdit.clear()


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MyWindow()
    window.show()
    sys.exit(app.exec_())